package cn.fengwoo.springBoot.controller;

import cn.fengwoo.springBoot.entity.Result;
import cn.fengwoo.springBoot.enums.ResultEnum;
import cn.fengwoo.springBoot.utils.ResultUtil;
import cn.fengwoo.springBoot.utils.frequent.BaseController;
import cn.fengwoo.springBoot.utils.frequent.FileUtil;
import cn.fengwoo.springBoot.utils.frequent.Util;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 微信支付API
 * Created by FancyFong on 18/1/2018.
 */
@RestController
@RequestMapping("/wxpay")
@Api(value = "/WxPayController", description = "微信支付管理")
public class WxPayController extends BaseController{

    @Value("${wechat.pay.notifyURL}")
    private String notifyURL;

    @Resource(name = "wxPayService")
    private WxPayService wxService;


    @ApiOperation(value="统一下单")
    @PostMapping("/createOrder")
    public Result<WxPayMpOrderResult> createOrder(
            @ApiParam(value = "标题",required = true) @RequestParam(value = "body", required = true) String body,
            @ApiParam(value = "订单号",required = true) @RequestParam(value = "outTradeNo", required = true) String outTradeNo
    ) throws Exception {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        WxPayUnifiedOrderRequest wxRequest = new WxPayUnifiedOrderRequest();
        wxRequest.setBody(body);
        wxRequest.setOutTradeNo(outTradeNo);
        wxRequest.setSpbillCreateIp(Util.getIpAddr(request));
        wxRequest.setTotalFee(100);
        wxRequest.setTradeType("APP");
        wxRequest.setNotifyURL(notifyURL);

        Object result = this.wxService.createOrder(wxRequest);
        return ResultUtil.success(ResultEnum.SUCCESS,result);
    }

    /**
     * 微信异步回调接口
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @ApiOperation(value="异步回调")
    @PostMapping("/weixin_notify")
    public void weixinNotify(HttpServletRequest request, HttpServletResponse response) throws IOException, WxPayException {
        ServletInputStream in = request.getInputStream();
        String resxml = FileUtil.readInputStream2String(in);

        WxPayOrderNotifyResult wxPayOrderNotifyResult = this.wxService.parseOrderNotifyResult(resxml);

    }

}